<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
          <el-button type="primary" :class="['btn']" @click="addclassify">添加分类</el-button>

          <!--        表格区域-->
          <tree-table :data="getGoodsListDataLevel" :columns="columns" :expand-type="false" border  :show-row-hover="false" :selection-type="false" >
              <template slot="isok" slot-scope="scope">
                <i class="el-icon-circle-check" v-if="scope.row.cat_deleted === false" style="color: green"></i>
                <i class="el-icon-circle-close" v-else style="color: red"></i>
              </template>


          <!--              排序-->
            <template slot="px" slot-scope="scope">
              <el-tag type="success" v-if="scope.row.cat_level === 0">标签一</el-tag>
              <el-tag type="info" v-else-if="scope.row.cat_level === 1">标签二</el-tag>
              <el-tag type="warning" v-else>标签三</el-tag>
            </template>

          <!--            操作-->
            <template slot="cz" slot-scope="slot">
              <el-button type="primary" icon="el-icon-edit" size="small" @click="editCategories(slot.row.cat_id)">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete" size="small"  @click="deleteCategories(slot.row.cat_id)">删除</el-button>
            </template>
          </tree-table>

          <!--          分页区域-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getGoodsListDataLevel.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="getGoodsListDataLevel.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!--    添加分类的弹出层-->
    <el-dialog
      title="添加分类"
      :visible.sync="addclassifydialogVisible"
      width="50%">
        <el-form :model="addClassify" :rules="rules" ref="ruleForm" class="demo-ruleForm">
          <el-form-item label="分类名称" prop="cat_name">
            <el-input v-model="addClassify.cat_name" ></el-input>
          </el-form-item>
          <el-form-item label="父级分类" >
            <el-cascader
              v-model="addvalue"
              :options="classify"
              :props="pzxz"
              @change="newclassify" size="medium " clearable >
            </el-cascader>
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="qxclassify">取 消</el-button>
    <el-button type="primary" @click="submitaddclassify">确 定</el-button>
    </span>
    </el-dialog>

    <!--    编辑分类的弹出层-->
    <el-dialog
      title="编辑"
      :visible.sync="editdialogVisible"
      width="50%">

      <el-form :inline="true" :model="byiddata">
        <el-form-item label="一级名称">
          <el-input v-model="byiddata.cat_name"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
    <el-button @click="editdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="editdata">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
    created() {
      // 调用获取商品分类函数
      this.getGoodsList()
    },
  data() {
    return {
      getGoodsListData: {
        pagenum: 1,
        pagesize: 5
      }
      ,
      addclassifydialogVisible: false,
      total: 0,
      getGoodsListDataLevel: [],
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      },{
        label: '是否有效',
        type: 'template',
        template: 'isok'
      },
        {
          label: '排序',
          type: 'template',
          template: 'px'
        }
        ,
        {
          label: '操作',
          type: 'template',
          template: 'cz'
        }
      ],
      rules: {
        cat_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ]
      },
      addClassify: {
        cat_name: null,
        cat_pid: 0,
        cat_level: 0
      },
      addvalue: '',
      classify: [],
      pzxz: {
        expandTrigger: 'hover',
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
      },
      editdialogVisible: false,
      // 存储根据id查询的分类
      byiddata: []
    }
  },
  methods: {
      async getGoodsList() {
        const {data: res} = await this.$http.get(`categories`,{
          params: this.getGoodsListData
        })
        if (res.meta.status !== 200) return this.$message.error('获取商品分类数据失败')
        this.getGoodsListDataLevel = res.data.result
        this.total = res.data.total
      },
    handleSizeChange(newsize) {
      this.getGoodsListData.pagesize = newsize
      this.getGoodsList()
    },
    handleCurrentChange(newCurrent) {
      this.getGoodsListData.pagenum = newCurrent
      this.getGoodsList()
    },
    addclassify() {
      this.addclassifydialogVisible = !this.addclassifydialogVisible
      this.getClassifyData()
    },
    async getClassifyData() {
        const {data: res} = await this.$http.get(`categories`,{
          params: { type: 2}
        })
      if (res.meta.status !== 200) return  this.$message.error('获取分类数据失败')
      this.classify = res.data
     },
    async newclassify(data) {
      if (this.addvalue.length > 0) {
        this.addClassify.cat_pid = this.addvalue[this.addvalue.length - 1]
        this.addClassify.cat_level = this.addvalue.length
        return
      }
      else {
        this.addClassify.cat_pid = 0
        this.addClassify.cat_level = 0
      }
    },
    async submitaddclassify() {
      this.addclassifydialogVisible = !this.addclassifydialogVisible
      const {data: res} = await this.$http.post(`categories`,this.addClassify)
      if (res.meta.status !== 201) return this.$message.error('添加分类失败')
      console.log(res)
      this.$message.success('添加成功')
      this.$refs.ruleForm.resetFields()
      this.addClassify.cat_level = 0
      this.addClassify.cat_pid = 0
      this.addClassify.cat_name = ''
      this.getGoodsList()
    },
    qxclassify() {
      this.addclassifydialogVisible = !this.addclassifydialogVisible
      this.$refs.ruleForm.resetFields()
      this.addClassify.cat_level = 0
      this.addClassify.cat_pid = 0
      this.addClassify.cat_name = ''
    },
    async deleteCategories(id) {
      const results = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      if (results !== 'confirm') return false
      const {data: res} = await this.$http.delete(`categories/${id}`)
      if (res.meta.status !== 200) return this.$message.error('删除失败')
      this.getGoodsList()
      this.$message.success('删除成功')
    },
    async editCategories(id) {
      this.editdialogVisible = !this.editdialogVisible
      // 获取id分类
      const {data: res} = await this.$http.get(`categories/${id}`)
      if (res.meta.status !== 200) return this.$message.error('获取分类数据失败')
      this.byiddata = res.data
    },
    async editdata() {
      this.editdialogVisible = false
      const {data: res} = await this.$http.put(`categories/${this.byiddata.cat_id}`,{cat_name: this.byiddata.cat_name})
      if (res.meta.status !== 200) return this.$message.error('编辑失败')
      this.$message.success('编辑成功')
      this.getGoodsList()
    }
  }
}
</script>

<style scoped>
.el-breadcrumb {
  margin: 0 0 15px 15px;
}
.el-pagination {
  margin-top: 10px;
}
.btn {
  margin-bottom: 20px;
}
.el-pagination {
  margin-top: 20px;
}
.el-cascader {
  width: 100%;
}
</style>
